package com.cw.jeeyt.service.impl.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiFacadeProjectInfoBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiFacadeProjectInfoDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeProjectInfoDo;
import com.cw.jeeyt.service.intf.api.ApiFacadeModuleInfoService;
import com.cw.jeeyt.service.intf.api.ApiFacadeProjectInfoService;
import com.cw.jeeyt.service.converter.api.ApiFacadeProjectInfoConverter;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.IdGenerator;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * api接口文档所在项目业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiFacadeProjectInfoServiceImpl implements ApiFacadeProjectInfoService {

    private static final Logger logger = LoggerFactory.getLogger(ApiFacadeProjectInfoServiceImpl.class);
    @Autowired
    private ApiFacadeProjectInfoDao apiFacadeProjectInfoDao;//api接口文档所在项目数据库操作接口
    @Autowired
    private ApiFacadeModuleInfoService apiFacadeModuleInfoService;//api项目模块业务接口

    /**
     * 查询所有api接口文档所在项目
     */
    @Override
    public List<ApiFacadeProjectInfoBo> queryApiFacadeProjectInfoList() throws Exception {

        List<ApiFacadeProjectInfoBo> facadeProjectInfoBos = ApiFacadeProjectInfoConverter.doList2BoList(
            apiFacadeProjectInfoDao.queryApiFacadeProjectInfoList());
        if (facadeProjectInfoBos != null && !facadeProjectInfoBos.isEmpty()) {
            for (ApiFacadeProjectInfoBo facadeProjectInfoBo : facadeProjectInfoBos) {
                facadeProjectInfoBo.setModuleInfoList(
                    apiFacadeModuleInfoService.queryListByProjectId(facadeProjectInfoBo.getId()));
            }
        }
        return facadeProjectInfoBos;
    }

    /**
     * 分页查询api接口文档所在项目
     *
     * @param apiFacadeProjectInfoBo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiFacadeProjectInfoBo> queryByPage(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo, Integer pageNo,
        Integer pageSize, Order order) throws Exception {
        PageInfo<ApiFacadeProjectInfoDo> pageInfo = apiFacadeProjectInfoDao.queryByPage(
            ApiFacadeProjectInfoConverter.bo2Do(apiFacadeProjectInfoBo), pageNo, pageSize, order);
        List<ApiFacadeProjectInfoBo> facadeProjectInfoBos = ApiFacadeProjectInfoConverter.doList2BoList(
            pageInfo.getList());
        if (CollectionUtil.isNotEmpty(facadeProjectInfoBos)) {
            for (ApiFacadeProjectInfoBo facadeProjectInfoBo : facadeProjectInfoBos) {
                facadeProjectInfoBo.setModuleInfoList(
                    apiFacadeModuleInfoService.queryListByProjectId(facadeProjectInfoBo.getId()));
            }
        }
        return new PageInfo<>(pageInfo, facadeProjectInfoBos);
    }

    /**
     * 根据主键查寻api接口文档所在项目信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeProjectInfoBo getApiFacadeProjectInfoById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        ApiFacadeProjectInfoBo apiFacadeProjectInfoBo = ApiFacadeProjectInfoConverter.do2Bo(
            apiFacadeProjectInfoDao.getById(id));
        apiFacadeProjectInfoBo.setModuleInfoList(apiFacadeModuleInfoService.queryListByProjectId(id));
        return apiFacadeProjectInfoBo;
    }

    /**
     * 新增api接口文档所在项目
     *
     * @param apiFacadeProjectInfoBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo) throws Exception {

        if (apiFacadeProjectInfoBo == null) {
            logger.error("api接口文档所在项目为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeProjectInfoDo apiFacadeProjectInfo = ApiFacadeProjectInfoConverter.bo2Do(apiFacadeProjectInfoBo);
        apiFacadeProjectInfo.setCreateBy(userVo.getId());
        apiFacadeProjectInfo.setCreateDate(new Date());
        apiFacadeProjectInfo.setUpdateBy(userVo.getId());
        apiFacadeProjectInfo.setUpdateDate(new Date());
        apiFacadeProjectInfo.setId(IdGenerator.nextId());
        Integer result = apiFacadeProjectInfoDao.insert(apiFacadeProjectInfo);
        return result;
    }

    /**
     * 新增api接口文档所在项目不为空的字段
     *
     * @param apiFacadeProjectInfoBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo) throws Exception {

        if (apiFacadeProjectInfoBo == null) {
            logger.error("api接口文档所在项目为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeProjectInfoDo apiFacadeProjectInfo = ApiFacadeProjectInfoConverter.bo2Do(apiFacadeProjectInfoBo);
        apiFacadeProjectInfo.setCreateBy(userVo.getId());
        apiFacadeProjectInfo.setCreateDate(new Date());
        apiFacadeProjectInfo.setUpdateBy(userVo.getId());
        apiFacadeProjectInfo.setUpdateDate(new Date());
        apiFacadeProjectInfo.setId(IdGenerator.nextId());
        Integer result = apiFacadeProjectInfoDao.insertSelective(apiFacadeProjectInfo);
        return result;
    }

    /**
     * 更新api接口文档所在项目
     *
     * @param apiFacadeProjectInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo) throws Exception {

        if (apiFacadeProjectInfoBo == null) {
            logger.error("api接口文档所在项目为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeProjectInfoDo apiFacadeProjectInfo = ApiFacadeProjectInfoConverter.bo2Do(apiFacadeProjectInfoBo);
        apiFacadeProjectInfo.setUpdateBy(userVo.getId());
        apiFacadeProjectInfo.setUpdateDate(new Date());

        apiFacadeProjectInfoDao.update(apiFacadeProjectInfo);
    }

    /**
     * 更新api接口文档所在项目不为空的字段
     *
     * @param apiFacadeProjectInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo) throws Exception {

        if (apiFacadeProjectInfoBo == null) {
            logger.error("api接口文档所在项目为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeProjectInfoDo apiFacadeProjectInfo = ApiFacadeProjectInfoConverter.bo2Do(apiFacadeProjectInfoBo);
        apiFacadeProjectInfo.setUpdateBy(userVo.getId());
        apiFacadeProjectInfo.setUpdateDate(new Date());

        apiFacadeProjectInfoDao.updateSelective(apiFacadeProjectInfo);
    }

    /**
     * 删除api接口文档所在项目
     *
     * @param apiFacadeProjectInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo) throws Exception {

        if (apiFacadeProjectInfoBo == null) {
            logger.error("api接口文档所在项目为空");
            return;
        }
        apiFacadeProjectInfoDao.delete(ApiFacadeProjectInfoConverter.bo2Do(apiFacadeProjectInfoBo));
    }

    /**
     * 批量保存接口项目信息
     */
    @Override
    public void saveApiProjectInfoBat(List<ApiFacadeProjectInfoBo> apiFacadeProjectInfoList) throws Exception {
        if (apiFacadeProjectInfoList == null || apiFacadeProjectInfoList.isEmpty()) {
            logger.error("接口项目信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        List<ApiFacadeProjectInfoDo> projectInfoList = new ArrayList<ApiFacadeProjectInfoDo>();
        for (ApiFacadeProjectInfoBo projectInfo : apiFacadeProjectInfoList) {
            ApiFacadeProjectInfoDo apiFacadeProjectInfo = ApiFacadeProjectInfoConverter.bo2Do(projectInfo);
            apiFacadeProjectInfo.setCreateBy(userVo.getId());
            apiFacadeProjectInfo.setCreateDate(new Date());
            apiFacadeProjectInfo.setUpdateBy(userVo.getId());
            apiFacadeProjectInfo.setUpdateDate(new Date());
            projectInfoList.add(apiFacadeProjectInfo);
        }
        Integer result = apiFacadeProjectInfoDao.insertBat(projectInfoList);
    }

    /**
     * 根据项目编码查询项目信息
     */
    @Override
    public List<ApiFacadeProjectInfoBo> queryListByProjectCodes(List<String> projectCodes) throws Exception {
        if (projectCodes == null || projectCodes.isEmpty()) {
            logger.error("项目编码为空");
            return null;
        }
        List<ApiFacadeProjectInfoBo> facadeProjectInfoBos = ApiFacadeProjectInfoConverter.doList2BoList(
            apiFacadeProjectInfoDao.queryListByProjectCodes(projectCodes));
        if (facadeProjectInfoBos != null && !facadeProjectInfoBos.isEmpty()) {
            for (ApiFacadeProjectInfoBo facadeProjectInfoBo : facadeProjectInfoBos) {
                facadeProjectInfoBo.setModuleInfoList(
                    apiFacadeModuleInfoService.queryListByProjectId(facadeProjectInfoBo.getId()));
            }
        }
        return facadeProjectInfoBos;
    }
}
